package com.base.vistter.iframe.inverter.controller;

import com.base.vistter.iframe.annotation.Log;
import com.base.vistter.iframe.bean.Result;
import com.base.vistter.iframe.constant.CommonConstant;
import com.base.vistter.iframe.entity.SysFirm;
import com.base.vistter.iframe.entity.SysUser;
import com.base.vistter.iframe.exception.IframeException;
import com.base.vistter.iframe.inverter.model.LoginRequest;
import com.base.vistter.iframe.inverter.model.LoginResponse;
import com.base.vistter.iframe.menu.BusinessType;
import com.base.vistter.iframe.menu.ErrorLoginEnum;
import com.base.vistter.iframe.menu.OperatorType;
import com.base.vistter.iframe.model.LoginUserModel;
import com.base.vistter.iframe.properties.IframeProperties;
import com.base.vistter.iframe.service.SysFirmService;
import com.base.vistter.iframe.utils.JwtUtils;
import com.base.vistter.iframe.utils.RedisUtils;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
public class LoginController {

    @Autowired
    public AuthenticationManager authenticationManager;
    @Resource(name = "sysFirmService")
    private SysFirmService sysFirmService;
    @Resource(name = "iframeProperties")
    private IframeProperties iframeProperties;

    @Log(title = "用户登录", businessType = BusinessType.LOGIN, operatorType = OperatorType.MOBILE)
    @PostMapping(value = "/login", produces = "application/json;charset=utf-8")
    public Result<LoginResponse> login(@RequestBody @Valid LoginRequest loginRequest) throws IframeException {

        UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(loginRequest.getUsername(), loginRequest.getPassword());
        Authentication authentication = authenticationManager.authenticate(auth);
        SysUser sysUser = (SysUser) authentication.getPrincipal();
        SysFirm sysFirm = sysFirmService.getById(sysUser.getSysFirmId());
        if (1 == sysFirm.getStatus()) {
            return Result.error(ErrorLoginEnum.FIRM_ISSTOP);
        }
        SecurityContextHolder.getContext().setAuthentication(authentication);
        LoginUserModel loginUserModel = new LoginUserModel(sysUser.getId(), sysUser.getUsername(), sysUser.getSysDepartId(), sysUser.getSysFirmId(), sysUser.getSysRoleId(), sysUser.getIsAdmin());
        String token = JwtUtils.createToken(loginUserModel, iframeProperties.getSignatureSecret());
        RedisUtils.setToken(token, iframeProperties.getTimeout(), TimeUnit.MINUTES);
        return Result.ok(LoginResponse.builder().build().setToken(token).setSysUser(sysUser));

    }
    @GetMapping(value = "/logout")
    public Result<Boolean> logout(HttpServletRequest request) throws IframeException {
        String authorization = request.getHeader(CommonConstant.AUTHORIZATION);
        if(StringUtils.isNotEmpty(authorization)){
            authorization = authorization.replace(CommonConstant.BEARER + " ", "");
            RedisUtils.removeToken(authorization);
            return Result.ok(true);
        }
        return Result.ok(false);
    }

}
